package iosr11.ui.tags;

import iosr11.data.Student;
import iosr11.ui.data.StudentBean;

import java.io.IOException;
import java.util.logging.Logger;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;


public class UIStudentInfo extends UIOutput{
	private Logger logger = Logger.getLogger("iosr.ui.tags");
	static final String labelClass = "labelText";
	static final String valueClass = "normalText";
	
	public UIStudentInfo() {
		setRendererType(null);
	}
	
	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientID = getClientId(context);
		
		
		String name="";
		int indexNr=0;
		
		Student student = (Student)getAttributes().get("student"); 
		if(student!=null){
			if(student.getName()!=null){ name = student.getName(); }
			indexNr = student.getIndexNumber(); 
		}
		
		writer.startElement("table", this);
		writer.write("<tr><td colspan=\"2\" class=\"studentInfoTitle\">Dane studenta</td></tr>");
		
		writer.write("<tr>");
		writeCell(writer, "Imie", labelClass);
		writeCell(writer, name, valueClass);
		writer.write("</tr><tr>");
		writeCell(writer, "Nr indeksu", labelClass);
		writeCell(writer, new Integer(indexNr).toString(), valueClass);
		writer.write("</tr>");
		writer.endElement("table");
	}
	
	private void writeCell(ResponseWriter writer, String value, String className){
		try {
			writer.write(String.format("<td class=\"%s\">%s</td>", className, value));
		} catch (IOException e) {
			logger.severe("Cannot write StudentInfo cell value");
		}
	} 
}
